/*
 * Copyright (C) 2005-2007 Oleh Hapon ohapon@users.sourceforge.net
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Oleh Hapon
 * Kyiv, UKRAINE
 * ohapon@users.sourceforge.net
 */

/*
 * Created on 22.02.2004
 *
 */
package com.deruan.hotcore.common.service;

import java.util.HashMap;

import com.deruan.hotcore.common.exception.ServiceFinderException;



/**
 * @author Oleh Hapon $Id$
 * 
 * 
 * Service factory for find service.
 * 
 */
public class ServiceFactory {

    public static final int SMP = 0;

    public static final int EJB = 1;

    private static int typeDelegate;

    private static HashMap mapper;

    private static ServiceLocator serviceLocator;

    static {
	typeDelegate = SMP;
	mapper = new HashMap();
    }

    public static void put(Class key, Class value) {
	mapper.put(key, value);
    }

    public static ServiceLocator getServiceLocator() {
	return serviceLocator;
    }

    public static void setServiceLocator(ServiceLocator serviceLocator) {
	ServiceFactory.serviceLocator = serviceLocator;
    }

    public static Object getService(Class serviceInterface) 
    {
	try {
	    if (serviceLocator != null) {
		return serviceLocator.getService(serviceInterface);
	    }
	    Object vClass = mapper.get(serviceInterface);
	    if (vClass == null) {
		throw new ServiceFinderException("Service [" + serviceInterface
			+ "] not found");
	    }
	    return ((Class) vClass).newInstance();
	} catch (ServiceFinderException e) {
	    e.printStackTrace();
	    // throw e;
	} catch (IllegalAccessException e) {
	    e.printStackTrace();
	    // throw new BIZFinderException(e);
	} catch (InstantiationException e) {
	    e.printStackTrace();
	    // throw new BIZFinderException(e);
	}
	return null;
    }

    public static int getTypeDelegate() 
    {
	    return typeDelegate;
    }

    public static void setTypeDelegate(int typeDelegate) 
    {
	    ServiceFactory.typeDelegate = typeDelegate;
    }

}
